package com.bhs.sansong.ui.setting;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.CountDownTimerUtils2;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.SharedPreferencesUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 更改手机号
 */
public class ChangePhoneActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private EditText mTvPhone;
    private EditText mTvCode;
    private TextView mTvGetCode;
    private String phone;
    private Button mBtnSubmit;

    @Override
    public int initLayout() {
        return R.layout.activity_change_phone;
    }

    @Override
    public void initView() {
        phone = SharedPreferencesUtils.getString(mActivity, "phone");
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("更换手机号");
        mTvPhone = findViewById(R.id.tv_phone);
        mTvCode = findViewById(R.id.tv_code);
        mTvGetCode = findViewById(R.id.tv_getCode);
        mTvGetCode.setOnClickListener(this);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
        mTvPhone.setFocusableInTouchMode(false);
    }

    @Override
    public void initData() {
        mTvPhone.setText(MyUtils.phoneHide(phone));
        mTvCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                boolean verify = editable.length() > 0;
                mBtnSubmit.setEnabled(verify);
                mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_getCode) {
            networkRequest(netApi.getCode("1", phone), this);
        } else if (view.getId() == R.id.btn_submit) {
            if (mBtnSubmit.getText().toString().equals("立即验证")) {
                networkRequest(netApi.check_old_mobile(phone, mTvCode.getText().toString()), this, 9527);
            } else {
                if (!MyUtils.isPhone(mTvPhone.getText().toString())) {
                    ToastUtil.show("请输入正确的手机号码");
                    return;
                }
                networkRequest(netApi.bind_new_mobile(phone, mTvCode.getText().toString()), this, 9528);
            }
        }

    }

    CountDownTimerUtils2 countDownTimerUtils2;

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {

        if (requestCode == 0) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            if (bean.isStatus()) {
                if (countDownTimerUtils2 == null) {
                    countDownTimerUtils2 = new CountDownTimerUtils2(mTvGetCode, 60 * 1000, 1000);
                }
                countDownTimerUtils2.start();
            } else {
                ToastUtil.show(bean.getMsg());
            }
        } else if (requestCode == 9527) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                mTvPhone.setText("");
                mTvPhone.setFocusableInTouchMode(true);
                mTvCode.setText("");
                mBtnSubmit.setText("确认");
                mTvGetCode.setText("获取验证码");
                mTvGetCode.setClickable(true);
                countDownTimerUtils2.cancel();
            }
        } else if (requestCode == 9528) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                SharedPreferencesUtils.saveString(mActivity, "phone", mTvPhone.getText().toString());
                finish();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}